tywx.TCPClient = {
    CONNECT_STATUS_OK: 1,
    CONNECT_STATUS_OPENING: 2,
    CONNECT_STATUS_CLOSING: 3,
    CONNECT_STATUS_FAIL: 0,
    connectStatus: 0,
    isTimerInited: !1,
    tickCount: 0,
    filterCmds: ["test"],
    timerSchedule: function () {
        tywx.TCPClient.tickCount = (tywx.TCPClient.tickCount + 1) % 3, 2 == tywx.TCPClient.tickCount && tywx.TCPClient.connectStatus == tywx.TCPClient.CONNECT_STATUS_OK && tywx.NotificationCenter.trigger(tywx.EventType.SEND_HEART_BEAT),
            tywx.TCPClient.reconnet();
    },
    startCheckTimer: function () {
        tywx.TCPClient.isTimerInited = !0, tywx.Timer.setTimer(cc.director, this.timerSchedule, 1);
    },
    stopCheckTimer: function () {
        tywx.TCPClient.isTimerInited = !1, tywx.Timer.cancelTimer(cc.director, this.timerSchedule);
    },
    connect: function (t) {
        tywx.TCPClient.connectStatus != tywx.TCPClient.CONNECT_STATUS_OPENING && tywx.TCPClient.connectStatus != tywx.TCPClient.CONNECT_STATUS_OK && (tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_OPENING,
            wx.connectSocket({
                url: t
            }), wx.onSocketOpen(function (t) {
            wxapp.twarn("tcp", "TCP webSocket opened..."), tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_OK,
                tywx.NotificationCenter.trigger(tywx.EventType.TCP_OPENED), tywx.TCPClient.isTimerInited || tywx.TCPClient.startCheckTimer();
        }), wx.onSocketError(function (t) {
            tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL, tywx.NotificationCenter.trigger(tywx.EventType.TCP_ERROR);
        }), wx.onSocketClose(function (t) {
            wxapp.twarn("tcp", "WebSocket 已关闭！"), tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL,
                tywx.NotificationCenter.trigger(tywx.EventType.TCP_CLOSE);
        }), wx.onSocketMessage(function (t) {
            if (tywx.StateInfo.isOnForeground) {
                var e = tywx.TCPClient.decodeMessage(t.data);
                if (null != e && "0000" != e) {
                    var n = e.substr(0, e.length - 0);
                    if (null != n && n.length > 0) {
                        var C = JSON.parse(n);
                        tywx.TCPClient.filterCmds.indexOf(C.cmd), C.cmd, tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECEIVE, C);
                    }
                }
            }
        }));
    },
    decodeMessage: function (t) {
        if ("undefined" != typeof ArrayBuffer && t instanceof ArrayBuffer) {
            for (var e = new Uint8Array(t), n = "", C = 0, i = e.length; C < i; C++) n += String.fromCharCode(e[C]);
            return n;
        }
        for (var T = (t = tywx.EncodeDecode.base64Decode(t)).slice(0, 4), C = 0, i = (t = t.slice(4)).length; C < i; C++) {
            var c = t[C];
            c ^= T[C % 4], t[C] = c;
        }
        return tywx.EncodeDecode.utf8Decode(t);
    },
    reconnet: function () {
        tywx.StateInfo.isOnForeground && tywx.TCPClient.connectStatus == tywx.TCPClient.CONNECT_STATUS_FAIL && (tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECONNECT),
            tywx.TCPClient.connect(tywx.SystemInfo.webSocketUrl), wxapp.twarn("net", "reconnet"));
    },
    sendMsg: function (t) {
        if (tywx.TCPClient.connectStatus == tywx.TCPClient.CONNECT_STATUS_OK) {
            var e = JSON.stringify(t);
            tywx.TCPClient.filterCmds.indexOf(t.cmd), wx.sendSocketMessage({
                data: e,
                success: function (e) {
                    t.cmd, tywx.EventType.CMD_HEART_BEAT;
                },
                fail: function (t) {
                    var e = arguments[0];
                    e && "sendSocketMessage:fail taskID not exist" === e.errMsg && (wx.closeSocket(),
                        tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL);
                },
                complete: function (t) {
                }
            });
        }
    },
    close: function () {
        tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_CLOSING, wx.closeSocket(),
            tywx.TCPClient.stopCheckTimer();
    }
};